package org.csu.newpetstoreback.controller.front;

import org.csu.newpetstoreback.common.CommonResponse;
import org.csu.newpetstoreback.entity.Category;
import org.csu.newpetstoreback.entity.Item;
import org.csu.newpetstoreback.entity.Product;
import org.csu.newpetstoreback.service.CatalogService;
import org.csu.newpetstoreback.vo.ItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/catalog/")
public class CatalogController {

    @Autowired
    private CatalogService catalogService;

    @GetMapping("categories")
    @ResponseBody
    public CommonResponse<List<Category>> getCategories(){
        return catalogService.getCategories();
    }

    @GetMapping("categories/{id}")
    @ResponseBody
    public CommonResponse<Category> getCategoryById(@PathVariable("id") String categoryId){
        return catalogService.getCategoryById(categoryId);
    }

    @GetMapping("categories/{id}/products")
    @ResponseBody
    public CommonResponse<List<Product>> getProductsByCategoryId(@PathVariable("id") String categoryId){
        return catalogService.getProductsByCategory(categoryId);
    }

    @GetMapping("products/{id}")
    @ResponseBody
    public CommonResponse<Product> getProductById(@PathVariable("id") String productId){
        return catalogService.getProductById(productId);
    }

    @GetMapping("products/{id}/items")
    @ResponseBody
    public CommonResponse<List<ItemVO>> getItemsByProductId(@PathVariable("id") String productId){
        return catalogService.getItemsByProductId(productId);
    }

    @GetMapping("items/{id}")
    @ResponseBody
    public CommonResponse<ItemVO> getItemById(@PathVariable("id") String itemId){
        return catalogService.getItemById(itemId);
    }

    @PostMapping("searchProduct")
    @ResponseBody
    CommonResponse <List<Product>>searchProductList(@RequestParam("keyword") String keyword){
        return catalogService.searchProductList(keyword);
    }












    }
